<?php

/**
 * Created by PhpStorm.
 * User: fanfzj
 * Date: 2015/4/7
 * Time: 21:22
 */
class Label_model extends CI_Model
{

    var $label_id = '';
    var $label_name = '';

    function __construct()
    {
        parent::__construct();
    }

    /*显示标签全部列表*/
    function show_label_list()
    {
        $this->db->cache_on();
        $query = $this->db->get('label_list');
        $data = $query->result_array();
        return $data;
    }

    /*分页显示标签列表*/
    function show_label_list_info($page, $limit)
    {
//        $query = $this->db->query('select label_list.*,count(word.id) as word_num from label_list left join word on label_list.label_id=word.label_id group by label_list.label_id; ORDER BY label_id DESC limit ' . ($page * $limit) . ',' . $limit);
        $query = $this->db->select('label_list.*')->select('count(word.id) as word_num')->join('word', 'label_list.label_id=word.label_id', 'left')->group_by('label_list.label_id')->order_by('label_id', 'asc')->limit($limit, ($page * $limit))->get('label_list');
        //echo $this->db->last_query();
        $data = $query->result_array();
        return array(
            'data' => $data,
            'total' => $this->db->count_all_results('label_list'),
            'data_num' => count($data),
        );
    }
    /*更新标签*/
    /*更新标签记录失败，日志信息添加功能 2015-5-1*/
    function update_label($id, $label_name)
    {
        $data = array(
            'label_name' => $label_name
        );
        $bool = $this->db->update('label_list', $data, array('label_id' => $id));
        if ($bool) {
            //更新成功
            //echo $this->db->last_query();
            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '更新标签，记录失败。试图修改的标签id为' . $id . ',标签名修改为', $label_name . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }
    /*添加标签*/
    /*添加标签记录失败，日志信息添加功能 2015-5-1*/
    function insert_label($label_name)
    {

        $data = array(
            'label_name' => $label_name
        );
        $bool = $this->db->insert('label_list', $data);
        if ($bool) {
            return $this->db->insert_id();
        } else {
            //数据库错误
            $system_blog = array(
                'text' => '添加标签，记录失败。添加的标签名为' . $label_name . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }
    /*删除标签*/
    /*删除标签记录失败，日志信息添加功能 2015-5-1*/
    function delete_label($id)
    {
        $bool = $this->db->delete('label_list', array('label_id' => $id));
        if ($bool) {
            //更新成功
            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '删除标签，记录失败。标签id为' . $id . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }
}